﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Modules;
using anaii.EasyNews.Data;
using System.Collections;
using anaii.EasyNews.Controller;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Security;
using System.Threading;
using DotNetNuke.Services.Localization;
using DotNetNuke.Common;

namespace anaii.EasyNews
{
    public partial class ViewList : PortalModuleBase, IActionable
    {
        #region Variables


        #endregion

        #region Base

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        #endregion

        #region Events

        public void pager_Command(object sender, CommandEventArgs e)
        {
            int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
            pager1.CurrentIndex = currnetPageIndx;
            BindData();
        }

        #endregion

        #region Private methods

        private void BindData()
        {
            litOutput.Text = "";

            EasyNewsDBDataContext db = new EasyNewsDBDataContext();

            ModuleController controller = new ModuleController();
            Hashtable TabModuleSettingsCurrent = controller.GetTabModuleSettings(this.TabModuleId);

            int NewsType = 0;
            int NewsStatus = 1;
            int NumberOfItemsPerPage = 10;
            bool ShowPaging = false;
            if (!string.IsNullOrEmpty((string)TabModuleSettingsCurrent["NewsListSelectedNewsType"]))
            {
                if (!int.TryParse(TabModuleSettingsCurrent["NewsListSelectedNewsType"].ToString(), out NewsType))
                    NewsType = 0;
            }
            if (!string.IsNullOrEmpty((string)TabModuleSettingsCurrent["NewsListSelectedNewsStatus"]))
            {
                if (!int.TryParse(TabModuleSettingsCurrent["NewsListSelectedNewsStatus"].ToString(), out NewsStatus))
                    NewsStatus = 1;
            }
            if (!string.IsNullOrEmpty((string)TabModuleSettingsCurrent["NewsListNumberofNewsPerPage"]))
            {
                if (!int.TryParse(TabModuleSettingsCurrent["NewsListNumberofNewsPerPage"].ToString(), out NumberOfItemsPerPage))
                    NumberOfItemsPerPage = 10;
            }
            if (!string.IsNullOrEmpty((string)TabModuleSettingsCurrent["NewsListShowPaging"]))
            {
                if (!bool.TryParse(TabModuleSettingsCurrent["NewsListShowPaging"].ToString(), out ShowPaging))
                    ShowPaging = false;
            }

            pager1.Visible = ShowPaging;
            pager1.PageSize = NumberOfItemsPerPage;

            pager1.GoClause = Localization.GetString("GoClause.Text", Globals.ApplicationPath + "/DesktopModules/anaii.EasyNews/App_LocalResources/View");
            pager1.OfClause = Localization.GetString("OfClause.Text", Globals.ApplicationPath + "/DesktopModules/anaii.EasyNews/App_LocalResources/View");
            pager1.FromClause = Localization.GetString("FromClause.Text", Globals.ApplicationPath + "/DesktopModules/anaii.EasyNews/App_LocalResources/View");
            pager1.PageClause = Localization.GetString("PageClause.Text", Globals.ApplicationPath + "/DesktopModules/anaii.EasyNews/App_LocalResources/View");
            pager1.ToClause = Localization.GetString("ToClause.Text", Globals.ApplicationPath + "/DesktopModules/anaii.EasyNews/App_LocalResources/View");
            pager1.ShowingResultClause = Localization.GetString("ShowingResultClause.Text", Globals.ApplicationPath + "/DesktopModules/anaii.EasyNews/App_LocalResources/View");
            pager1.ShowResultClause = Localization.GetString("ShowResultClause.Text", Globals.ApplicationPath + "/DesktopModules/anaii.EasyNews/App_LocalResources/View");
            pager1.BackToFirstClause = Localization.GetString("BackToFirstClause.Text", Globals.ApplicationPath + "/DesktopModules/anaii.EasyNews/App_LocalResources/View");
            pager1.GoToLastClause = Localization.GetString("GoToLastClause.Text", Globals.ApplicationPath + "/DesktopModules/anaii.EasyNews/App_LocalResources/View");
            pager1.BackToPageClause = Localization.GetString("BackToPageClause.Text", Globals.ApplicationPath + "/DesktopModules/anaii.EasyNews/App_LocalResources/View");
            pager1.NextToPageClause = Localization.GetString("NextToPageClause.Text", Globals.ApplicationPath + "/DesktopModules/anaii.EasyNews/App_LocalResources/View");
            pager1.LastClause = Localization.GetString("LastClause.Text", Globals.ApplicationPath + "/DesktopModules/anaii.EasyNews/App_LocalResources/View");
            pager1.FirstClause = Localization.GetString("FirstClause.Text", Globals.ApplicationPath + "/DesktopModules/anaii.EasyNews/App_LocalResources/View");
            pager1.PreviousClause = Localization.GetString("PreviousClause.Text", Globals.ApplicationPath + "/DesktopModules/anaii.EasyNews/App_LocalResources/View");
            pager1.NextClause = Localization.GetString("NextClause.Text", Globals.ApplicationPath + "/DesktopModules/anaii.EasyNews/App_LocalResources/View");
            int skip = (pager1.CurrentIndex - 1) * pager1.PageSize;
            int take = pager1.PageSize;

            string templateheader = (string)TabModuleSettingsCurrent["NewsListHeaderTemplate" + Thread.CurrentThread.CurrentCulture.Name];
            templateheader = EasyNewsController.ReplaceTokens(templateheader, new EasyNew());
            litOutput.Text += Server.HtmlDecode(templateheader);

            int counter = 0;
            int sum = 0;
            foreach (EasyNew currentNews in db.EasyNews.OrderByDescending(p => p.DateCreated))
            {
                bool add = true;
                if (NewsStatus == 1 && !currentNews.IsPublished)
                    add = false;
                if (NewsStatus == 2 && !currentNews.IsArchived)
                    add = false;
                if (NewsType != 0 && currentNews.TypeID != NewsType)
                    add = false;
                if (!currentNews.IsEnabled)
                    add = false;
                if (currentNews.ExpireDate.HasValue && DateTime.Compare(DateTime.Now, currentNews.ExpireDate.Value) >= 0)
                    add = false;
                if (currentNews.DatePublished.HasValue && DateTime.Compare(DateTime.Now, currentNews.DatePublished.Value) < 0)
                    if (!(UserInfo.UserID >= 1 && UserInfo.UserID <= 2))
                        add = false;

                if (add)
                {
                    sum++;
                    if (counter >= skip && take > 0)
                    {
                        take--;
                        string templateitem = (string)TabModuleSettingsCurrent["NewsListItemTemplate" + Thread.CurrentThread.CurrentCulture.Name];
                        templateitem = EasyNewsController.ReplaceTokens(templateitem, currentNews);
                        litOutput.Text += Server.HtmlDecode(templateitem);
                    }
                    counter++;
                }
            }

            pager1.ItemCount = sum;

            string templatefooter = (string)TabModuleSettingsCurrent["NewsListFooterTemplate" + Thread.CurrentThread.CurrentCulture.Name];
            templatefooter = EasyNewsController.ReplaceTokens(templatefooter, new EasyNew());
            litOutput.Text += Server.HtmlDecode(templatefooter);

        }

        #endregion

        #region IActionable Members

        public DotNetNuke.Entities.Modules.Actions.ModuleActionCollection ModuleActions
        {
            get
            {
                ModuleActionCollection Actions = new ModuleActionCollection();
                Actions.Add(GetNextActionID(), "Edit News", ModuleActionType.AddContent, "", "", EditUrl(), false, SecurityAccessLevel.Edit, true, false);
                return Actions;
            }
        }

        #endregion

    }
}